package com.atguigu.guli.service.video.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.ResultCodeEnum;
import com.atguigu.guli.service.base.Exception.GuliException;
import com.atguigu.guli.service.video.service.VideoService;
import com.atguigu.guli.service.video.util.AliyunVodSDKUtils;
import com.atguigu.guli.service.video.util.VideoProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * @author: Mr.Wang
 * @date: 2021-06-02 21:45
 * @description:
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoProperties videoProperties;

    @Override
    public String uploadVideo(InputStream inputStream, String originalFilename) {
        //获得视频名字
        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));

        UploadStreamRequest request = new UploadStreamRequest(
                videoProperties.getKeyid(),
                videoProperties.getKeysecret(),
                title, originalFilename, inputStream);
        /* 模板组ID(可选)
        request.setTemplateGroupId(vodProperties.getTemplateGroupId());
        工作流ID(可选)
        request.setWorkflowId(vodProperties.getWorkflowId());*/

        UploadVideoImpl uploadVideo = new UploadVideoImpl();
        UploadStreamResponse response = uploadVideo.uploadStream(request);
        String videoId = response.getVideoId();
        if (StringUtils.isBlank(videoId)) {
            log.error("上传失败"+ response.getCode() + '-' +response.getMessage());
            throw  new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
        return videoId;
    }

    @Override
    public void removeVideo(String videoId) throws Exception {
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(videoProperties.getKeyid(), videoProperties.getKeysecret());
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        client.getAcsResponse(request);
    }

    @Override
    public String getPlayAuth(String videoSourceId) throws Exception {
        //初始化client对象
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                videoProperties.getKeyid(),
                videoProperties.getKeysecret());

        //创建请求对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest ();
        request.setVideoId(videoSourceId);

        //获取响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        return response.getPlayAuth();
    }
}
